package com.bwhx.eruler.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.service.StaffService;
import com.bwhx.eruler.util.MD5Utils;
/**
 * 员工管理，销售员管理、库管管理
 * @author Cuilitang
 *
 */
@Controller
public class KuGuanController extends BaseController {
	
	@Autowired
	StaffService staffService;
	
	@ResponseBody
	@RequestMapping(value = "/page.kuguanList.ashx")
	 public Map staffList(Integer page,Integer rows,Staff s, @RequestBody String b) {
		
		if(!this.getSessionStaff().getsLoginName().equals("admin")){
			s.setsDeptId(this.getSessionStaff().getsDeptId());
		}
		 log.info(b);
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,s);
		Map map = new HashMap();
		try{
			List<Staff> list = staffService.list(p);
			Integer count = staffService.count(s);
			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			for (Staff staff : list) {
				log.info(staff);
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.kuguanListAll.ashx")
	 public List staffListAll() {
		List<Staff> list = new ArrayList<Staff>();
		try{
			Staff staff = new Staff();
/*			staff.setsDeptId(0);
*/			Page page = new Page();
			page.setSearchObj(staff);
			list = staffService.list(page);

		}catch(Exception e){
			log.error(getTrace(e));
				}
		
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.roleListAll.ashx")
	 public List<Staff> roleListAll() {
		List<Staff> list = new ArrayList<Staff>();
		
		try{
			Staff staff = new Staff();
			staff.setUserType(3);
     		Page page = new Page();
			page.setSearchObj(staff);
			list = staffService.list(page);

		}catch(Exception e){
			log.error(getTrace(e));
				}
		
	    return list;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateKuguan.ashx")
	public String saveOrUpdateStaff(Staff staff) {
		try{
			
		 	/*Staff s2 = staffService.findByPhone(staff.getsPhone());
			if(s2!=null&&s.getsId()!=staff.getsId()){
				return "conflict";
			}*/
			if(staff.getsId()==-1){				
				
				staff.setsId(null);
				String md5 = MD5Utils.md5(staff.getsPwd());
				staff.setsPwd(md5);
				staffService.insert(staff);
			}else{
				Staff dbstaff = staffService.get(staff.getsId());
				if(dbstaff.getsLoginName().equals("admin")&&(!staff.getsLoginName().equals("admin")||1==staff.getsStatus()||1!=staff.getUserType())){
					return "admin";
				}
				if(!dbstaff.getsPwd().equals(staff.getsPwd())){
					String md5 = MD5Utils.md5(staff.getsPwd());
					staff.setsPwd(md5);
				}
				staffService.update(staff);

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateRole.ashx")
	public String saveOrUpdateRole(Staff staff) {
		try{
			staff.setsName("role用户");
			staff.setUserType(3);
		 
			if(staff.getsId()==-1){
				
				Staff s = staffService.findByLoginName(staff.getsLoginName());
				if(s!=null){
					return "conflict";
				}
				
				staff.setsId(null);
 				staffService.insert(staff);
			}else{
				 
			 
				
				Staff s = staffService.findByLoginName(staff.getsLoginName());
				if(s!=null&&s.getsId()!=staff.getsId()){
					return "conflict";
				}
				staffService.update(staff);

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.staffdel.ashx")
 public Map<String,String> OfflineOrderDel(@RequestParam String sId){
	Map<String,String> resultMap = new HashMap<String, String>();
	
	staffService.del(sId);
	
	resultMap.put("status", "1");

    return resultMap;  
  }


	@ResponseBody
	@RequestMapping(value = "/page.staffGet.ashx")
	 public String get(String sId) {
		 String sName= "";
		try{
			Staff s = staffService.get(sId);
			sName = s.getsName();
			
		}catch(Exception e){
			log.error(getTrace(e));
		}
		log.info(sName);
	    return sName;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.roleList.ashx")
	 public Map<String,Object> roleList(Integer page,Integer rows,Staff s, @RequestBody String b) {
		
		 s.setUserType(3);
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,s);
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			List<Staff> list = staffService.list(p);
			Integer count = staffService.count(s);
			log.info(list);
			log.info(count);
			
			map.put("total",count );//total键 存放总记录数，必须的  
			map.put("rows",list);//rows键 存放每页记录 list 
			for (Staff staff : list) {
				log.info(staff);
			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }

}
